package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.domain.Order;
import com.itheima.reggie.domain.OrderDetail;
import com.itheima.reggie.mapper.OrderDetailMapper;
import com.itheima.reggie.mapper.OrderMapper;
import com.itheima.reggie.service.AddressService;
import com.itheima.reggie.service.CartService;
import com.itheima.reggie.service.OrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private AddressService addressService;
    @Autowired
    private CartService cartService;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    //提交订单
    @Override
    public void submit(Order order) {
        //1.查询数据备用
        //1.1查询address
        Address address = addressService.findById(order.getAddressId());
        //1.2查询购物车信息
        List<Cart> cartList = cartService.findList();
        //2.手动生成id,创建计数器备用
        long id = IdWorker.getId();
        BigDecimal sum = new BigDecimal(0);
        //3.遍历购物车，获取orderDetail
        for (Cart cart : cartList) {
            OrderDetail orderDetail = new OrderDetail();
            //将cart的大部分属性复制给orderDetail
            BeanUtils.copyProperties(cart,orderDetail);
            //补全参数
            orderDetail.setId(IdWorker.getId());
            orderDetail.setOrderId(id);
            //计数器累加sum+=number*amount
            sum = sum.add(orderDetail.getAmount().multiply(new BigDecimal(orderDetail.getNumber())));
            //添加到数据库
            orderDetailMapper.insert(orderDetail);
        }
        //4.补全order参数
        order.setId(id);    //跟orderDetail的OrderId保持一致
        order.setNumber(id+"");
        order.setStatus(1); //表示待付款
        order.setUserId(UserHolder.get().getId());
        order.setOrderTime(new Date());
        order.setCheckoutTime(new Date());
        order.setAmount(sum);
        order.setPhone(address.getPhone());
        order.setAddress(address.getDetail());
        order.setUserName(UserHolder.get().getName());
        order.setConsignee(address.getConsignee());
        orderMapper.insert(order);
        //5.清空购物车
        cartService.delete();
    }
    //分页查询
    @Override
    public Page<Order> findByPage(Integer pageNum, Integer pageSize) {
        //1.设置分页条件
        Page<Order> page = new Page<>(pageNum, pageSize);
        //2.设置业务条件
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Order::getUserId,UserHolder.get().getId())
                .orderByDesc(Order::getOrderTime);
        //3.执行查询
        page = orderMapper.selectPage(page, wrapper);
        if (CollectionUtil.isNotEmpty(page.getRecords())) {
            //4.遍历order,将orderDetail信息添加到order
            for (Order order : page.getRecords()) {
                LambdaQueryWrapper<OrderDetail> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(OrderDetail::getOrderId,order.getId());
                List<OrderDetail> orderDetailList = orderDetailMapper.selectList(wrapper1);
                order.setOrderDetails(orderDetailList);
            }
        }
        return page;
    }
    //分页条件查询
    @Override
    public Page<Order> findByPageFromManage(Integer pageNum, Integer pageSize,String number, Date beginTime, Date endTime) {
        //1.设置分页条件
        Page<Order> page = new Page<>(pageNum,pageSize);
        //2.设置业务条件
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(number),Order::getId,number)
                .between(beginTime!=null,Order::getOrderTime,beginTime,endTime);
        //3.查询
        page = orderMapper.selectPage(page, wrapper);
        return page;
    }
    //1.查询数据备用
    //1.1根据addressId查询address
//    Address address = addressService.findById(order.getAddressId());
//    //1.2查询当前用户的购物车列表
//    List<Cart> cartList = cartService.findList();
//    //2.手动生成orderId
//    long id = IdWorker.getId();
//    //创建计数器
//    BigDecimal sum = new BigDecimal(0);
//    //3.遍历购物车列表，添加订单详情
//        for (Cart cart : cartList) {
//        //创建orderDetail对象
//        OrderDetail orderDetail = new OrderDetail();
//        //将cart中的大部分属性复制到orderDetail中
//        BeanUtils.copyProperties(cart,orderDetail);
//        //单独设置orderDetail的id和orderId
//        orderDetail.setId(IdWorker.getId());
//        orderDetail.setOrderId(id);
//        //计算本菜品/套餐总金额：数量*单价
//        sum=sum.add(new BigDecimal(orderDetail.getNumber()).multiply(orderDetail.getAmount()));
//        //将对象添加进数据库
//        orderDetailMapper.insert(orderDetail);
//    }
//    //4.设置订单参数
//        order.setId(id);    //跟orderDetail的orderId一致
//        order.setNumber(id+"");
//        order.setStatus(1); //1待付款
//        order.setUserId(UserHolder.get().getId());
//        order.setOrderTime(new Date());
//        order.setCheckoutTime(new Date());
//        order.setAmount(sum);  //总金额
//        order.setPhone(address.getPhone());
//        order.setAddress(address.getDetail());
//        order.setUserName(UserHolder.get().getName());
//        order.setConsignee(address.getConsignee());
//    //添加到数据库
//        orderMapper.insert(order);
//    //5.清空购物车
//        cartService.delete();
}
